/**
 * $RCSfile$
 * $Revision$
 * $Date$
 * $Source$
 * $Author$
 */
package pl.edu.agh.loganalizator.presenter;

import java.awt.GraphicsConfiguration;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;

import pl.edu.agh.loganalizator.model.ColumnModel;
import pl.edu.agh.loganalizator.model.Row;
import pl.edu.agh.loganalizator.model.Rule;
import pl.edu.agh.loganalizator.model.RuleModel;

public class Parser {

  ArrayList<Row> rowArrayList;
  ColumnModel columnModel;
  RuleModel ruleModel;

  public Parser(File f) throws IOException {
    rowArrayList = new ArrayList<Row>();

    File file = f;
    FileReader freader = null;
    LineNumberReader lnreader = null;
    try {
      freader = new FileReader(file);
      lnreader = new LineNumberReader(freader);
      columnModel = new ColumnModel(lnreader.readLine());
      String line = "";
      while ((line = lnreader.readLine()) != null) {
        try {
          rowArrayList.add(columnModel.getRowValue(line));
        }
        catch (Exception e) {
          //System.out.println("nieistotna pusta linia");
        }
      }
    }
    catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    finally {
      freader.close();
      lnreader.close();
    }
  }

  public void parse() {
    for (Row row : rowArrayList) {
      if (!ruleModel.isParsePositive(row)) {
        row.setDisable();
      }
    }
  }

  public String getParsedText() {
    StringBuffer stringBuffer = new StringBuffer();
    for (Row row : rowArrayList) {
      stringBuffer.append(row.toString());
    }
    return stringBuffer.toString();
  }

  public void setRuleModel(RuleModel ruleModel) {
    this.ruleModel = ruleModel;
  }

  public RuleModel getRuleModel() {
    return ruleModel;
  }

  public ColumnModel getColumnModel() {
    // TODO Auto-generated method stub
    return columnModel;
  }
}
